﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RM2012Server.Model;
using System.Data;
using RM2012Server.Business;
using DevComponents.DotNetBar.Controls;
using System.Windows.Forms;

namespace RM2012Server.Controller
{
    public class StateCtrl
    {
        StateDAO m_StateDAO = new StateDAO();

        public void LoadToComboBox(ComboBoxEx cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "StateName";
            cmb.ValueMember = "StateId";
        }

        public void LoadToDataGridViewComboBoxColumn(DataGridViewComboBoxColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "StateName";
            cmb.ValueMember = "StateId";
            cmb.DataPropertyName = "StateId";
            cmb.HeaderText = "Trạng thái";
        }

        public void LoadToDataGridViewComboBoxExColumn(DataGridViewComboBoxExColumn cmb)
        {
            cmb.DataSource = GetAll();
            cmb.DisplayMember = "StateName";
            cmb.ValueMember = "StateId";
            cmb.DataPropertyName = "StateId";
            cmb.HeaderText = "Trạng thái";
        }

        public void Show(DataGridViewX dgv, BindingNavigator bn)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();
            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void Show(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtStateId,
                         TextBoxX txtStateName)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = GetAll();

            txtStateId.DataBindings.Clear();
            txtStateId.DataBindings.Add("Text", bs, "StateId");

            txtStateName.DataBindings.Clear();
            txtStateName.DataBindings.Add("Text", bs, "StateName");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public void ShowWithSources(DataGridViewX dgv,
                         BindingNavigator bn,
                         TextBoxX txtStateId,
                         TextBoxX txtStateName,
                         DataTable dtSource)
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = dtSource;

            txtStateId.DataBindings.Clear();
            txtStateId.DataBindings.Add("Text", bs, "StateId");

            txtStateName.DataBindings.Clear();
            txtStateName.DataBindings.Add("Text", bs, "StateName");

            bn.BindingSource = bs;
            dgv.DataSource = bs;
        }

        public DataTable GetAll()
        {
            return m_StateDAO.GetAll();
        }

        public DataTable GetById(int stateId)
        {
            return m_StateDAO.GetById(stateId);
        }

        public DataTable GetByName(String stateName)
        {
            return m_StateDAO.GetByName(stateName);
        }

        public int Add(StateInfo info)
        {
            return m_StateDAO.Add(info);
        }

        public int Update(StateInfo info)
        {
            return m_StateDAO.Update(info);
        }

        public int Delete(int stateId)
        {
            return m_StateDAO.Delete(stateId);
        }

        public DataRow AddNewRow()
        {
            return m_StateDAO.AddNewRow();
        }

        public void AddNewState(DataRow m_Row)
        {
            m_StateDAO.AddNewState(m_Row);
        }

        public bool SaveData()
        {
            return m_StateDAO.SaveData();
        }
    }
}
